using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Cuyahoga.Web.ComplexControlForTest
{
    public partial class WidgetListSub : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            rptWidgetList.DataSource = Data.GetAllWidgetInstances();
            rptWidgetList.DataBind();
        }

        protected void rptWidgetList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int widgetInstanceID = int.Parse(((LinkButton)e.CommandSource).CommandArgument);
            if (e.CommandName.ToLower().Equals("delete".ToLower()))
            {
                lblWidget.Visible = true;
                lblWidget.Text = "Widget Id = " + widgetInstanceID + ", delete";
            }
            else if (e.CommandName.ToLower().Equals("edit".ToLower()))
            {
                lblWidget.Visible = true;
                lblWidget.Text = "Widget Id = " + widgetInstanceID + ", edit";
            }
            else if (e.CommandName.ToLower().Equals("view".ToLower()))
            {
                lblWidget.Visible = true;
                lblWidget.Text = "Widget Id = " + widgetInstanceID + ", view";
            }
            else if (e.CommandName.ToLower().Equals("copy".ToLower()))
            {
                lblWidget.Visible = true;
                lblWidget.Text = "Widget Id = " + widgetInstanceID + ", copy";
            }

        }

        protected void rptWidgetList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            WidgetInstance ins = e.Item.DataItem as WidgetInstance;
            if (ins == null)
            {
                return;
            }
            LinkButton lbnEdit = e.Item.FindControl("lbnEdit") as LinkButton;
            if (lbnEdit != null)
            {
                lbnEdit.CommandArgument = ins.Id.ToString();
            }
            LinkButton lbnView = e.Item.FindControl("lbnView") as LinkButton;
            if (lbnView != null)
            {
                lbnView.CommandArgument = ins.Id.ToString();
            }
            LinkButton lbnDelete = e.Item.FindControl("lbnDelete") as LinkButton;
            if (lbnDelete != null)
            {
                lbnDelete.CommandArgument = ins.Id.ToString();
                string jsAlert = "return confirm('are you sure');";
                lbnDelete.Attributes.Add("onclick", jsAlert);
            }
            LinkButton lbnCopy = e.Item.FindControl("lbnCopy") as LinkButton;
            if (lbnCopy != null)
            {
                lbnCopy.CommandArgument = ins.Id.ToString();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToLongTimeString();
        }
    }
}